package com.offcn.bigdata.spark.sql.p3

import org.apache.spark.sql.SparkSession

/**
 * @Auther: BigData-LGW
 * @ClassName: SparkSQLUDF
 * @Date: 2020/12/10 20:24
 * @功能描述: $FunctionDescription
 * @Version:1.0
 */
object SparkSQLUDF {
    def main(args: Array[String]): Unit = {
        val spark = SparkSession.builder()
            .appName("SparkSQLUDF")
            .master("local[*]")
            .getOrCreate()
        spark.udf.register[Int, String]("myLen", str => udfs(str))
        val df = spark.read.json("file:/F:/people.json")
        df.createOrReplaceTempView("person")
        val sql =
            """
              |select
              |name,
              |length(name),
              |myLen(name) my_len
              |from person
              |""".stripMargin
        spark.sql(sql).show()
        spark.stop()
    }
    def udfs(str:String):Int=str.length
}
